"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpUtil = void 0;
const xml2js = require('xml2js');
const request = require('request');
const axios = require('axios');
class HttpUtil {
    static request_by_body(url, params, callback = null) {
        // Logger.log("request_by_body===req=", url, params)
        axios({
            method: 'post',
            url: url,
            timeout: 20000,
            headers: { 'X-Custom-Header': 'abcd' },
            data: params
        }).then(function (response) {
            // Logger.log("request_by_body===response=", response)
            callback && callback(response);
        });
    }
    static setParams(paramsObj) {
        let resParams = "";
        let nowIndex = 1;
        for (const key in paramsObj) {
            if (paramsObj.hasOwnProperty(key)) {
                let valueStr = paramsObj[key];
                if (nowIndex == 1) {
                    resParams += key + "=" + encodeURIComponent(valueStr);
                }
                else {
                    resParams += "&" + key + "=" + encodeURIComponent(valueStr);
                }
                nowIndex += 1;
            }
        }
        // Logger.log("setParams==============", paramsObj, resParams);
        return resParams;
    }
    static request_by_form(url, params = null, callback = null) {
        if (params) {
            url = url + "?" + this.setParams(params);
        }
        axios({
            method: 'get',
            url: url,
            timeout: 20000,
            headers: { 'X-Custom-Header': 'abcd' }
        }).then(function (response) {
            // Logger.log("request_by_form===response=", response)
            callback && callback(response);
        });
    }
    static request_by_form_promise(url, params = null) {
        return __awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve, reject) => {
                this.request_by_form(url, params, (respData) => {
                    resolve(respData);
                });
            });
        });
    }
    static request_by_body_promise(url, params = null) {
        return __awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve, reject) => {
                this.request_by_body(url, params, (respData) => {
                    resolve(respData);
                });
            });
        });
    }
}
exports.HttpUtil = HttpUtil;
